<!--
 - Author : 003.
 - Date   : 2018/9/25.
 - File   : 新建
 -->

<template>
  <div class="wrapper publish">
    <b-header></b-header>
    <div class="main-wrapper isno-left-bar">
      <div class="main-content">
        <div class="page">
          <div :class="'page-content'">
            <div class="table_wrap">
              <table class="table table-order table-order-hover">
                <thead>
                  <tr>
                    <th>序号</th>
                    <th>商品id</th>
                    <th style="width:120px;">商品名称</th>
                    <th>商品主图</th>
                    <th>商品价格(单位：元)</th>
                    <th>佣金</th>
                    <!-- <th>淘口令(30天内有效)</th> -->
                    <!-- <th>淘宝客短链接(300天内有效)</th>
                    <th>优惠券短链接(300天内有效)</th> -->
                    <th>优惠券淘口令(30天内有效)</th>
                    <th>优惠券总量</th>
                    <th>优惠券剩余量</th>
                    <th>优惠券面额</th>
                    <th>优惠券结束时间</th>
                  </tr>
                </thead>
                <tbody>
                  <tr v-for="(item, index) in orderList" :key="index">
                    <td style="width:60px">
                      <div class="ct" style="width:50px">{{index+1}}</div>
                    </td>
                    <td>
                      <div class="ct">{{item.productId}}</div>
                    </td>
                    <td style="width:120px;">
                      <div class="ct" style="width:120px;">{{item.productName}}</div>
                    </td>
                    <td>
                      <div class="ct"><img class="zhutu" :src="item.mainImg"/></div>
                    </td>
                    <td>
                      <div class="ct">{{item.productPrice}}</div>
                    </td>
                    <td>
                      <div class="ct">{{item.Commission}}</div>
                    </td>
                    <!-- <td>
                      <div class="ct">{{item.Passwords}}</div>
                    </td>
                    <td>
                      <div class="ct">{{item.shortLink}}</div>
                    </td>
                    <td>
                      <div class="ct">{{item.couponLink}}</div>
                    </td> -->
                    <td>
                      <div class="ct">{{item.couponPasswords}}</div>
                    </td>
                    <td>
                      <div class="ct">{{item.couponSum}}</div>
                    </td>
                    <td>
                      <div :class="['ct', item.couponRest ? '':'empty']">{{item.couponRest}}</div>
                    </td>
                    <td>
                      <div class="ct">{{item.couponPrice}}</div>
                    </td>
                    <td>
                      <div class="ct">{{item.endTime}}</div>
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
          <div class="page-foot">
              <div class="btn_file">
                <div class="file_box file_box1">
                  <i class="iconfont icon-Excel"></i>
                  <input
                    type="file"
                    id="excel-file"
                    name="upload"
                    class="file-btn"
                    @change="exportExcel"
                  >导入
                </div>
              </div>
            <span>总票价：{{priceCount}}</span>
          </div>
          <div class="fixed-ico" @click="back">
            <img src="../../assets/images/back-ico.png" alt>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { apiSource, get } from "../../travel-api"
import XLSX from "xlsx";
import config from "../../config"

export default {
  data() {
    return {
      priceCount: '',
      endTime: [],
      productId: "",
      orderList: [
        {
          productId: '',
          productName: "",
          mainImg: "",
          productPrice: "",
          Commission: "",
          shortLink: "",
          couponLink: "",
          Passwords: "",
          couponSum: "",
          couponRest: "",
          couponPrice: "",
          endTime: ""
        }
      ]
    }
  },
  watch: {
  },
  mounted() {
  },
  methods: {
    /**
     * 导入表格
     */
    exportExcel(e) {
      var self = this
      var files = e.target.files
      // var rABS = false
      var fileReader = new FileReader()
      fileReader.onload = function(ev) {
        try {
          var data = ev.target.result,
            workbook = XLSX.read(data, {
              type: "binary"
            }), // 以二进制流方式读取得到整份excel表格对象
            persons = [] // 存储获取到的数据
        } catch (e) {
          console.error(e)
          self.$message.error("文件类型不正确")
          return
        }
        var exportList = JSON.parse(
          JSON.stringify(
            XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]])
          )
        )
        var jsonList = []
        if (exportList.length > 0) {
          jsonList = exportList.map((item, index) => {
            return {
              productId: item["商品id"]
                ? item["商品id"].toString().replace(/^\s+|\s+$/g, "")
                : "",
              productName: item["商品名称"]
                ? item["商品名称"].toString().replace(/^\s+|\s+$/g, "")
                : "",
              mainImg: item["商品主图"],
              productPrice: item["商品价格(单位：元)"],
              Commission: item["佣金"],
              // Passwords: item["淘口令(30天内有效)"],
              // shortLink: item["淘宝客短链接(30天内有效)"],
              // couponLink: item["优惠券链接(30天内有效)"],
              couponPasswords: item["优惠券淘口令(30天内有效)"],
              couponSum: item["优惠券总量"],
              couponRest: item["优惠券剩余量"],
              couponPrice: item["优惠券面额"],
              endTime: item["优惠券结束时间"]
            }
          })
        }
        console.log("jsonList", jsonList, exportList)
        let orderList = self.orderList
        if (
          orderList.length === 1 &&
          !orderList[0].productId &&
          !orderList[0].productName &&
          !orderList[0].couponRest
        ) {
          //初始页面首行内容为空时导入
          self.orderList = jsonList
        } else {
          //导入去重
          let repeatJsonList = [] //重复数组
          orderList.forEach((fItem, f) => {
            repeatJsonList = repeatJsonList.concat(
              jsonList.filter((v, k) => {
                return (
                  v.productId === fItem.productId &&
                  v.productName === fItem.productName
                )
              })
            )
          })
          let filterJsonList = jsonList.filter(json => {
            //去重
            console.log("导入表格", repeatJsonList, json)
            return repeatJsonList.indexOf(json) === -1
          })
          self.orderList = self.orderList.concat(filterJsonList)
        }
        if (orderList.length > 0) {
          self.countResult()
        } else {
          self.$message.error("暂无数据导入")
        }
      }
      // 以二进制方式打开文件
      fileReader.readAsBinaryString(files[0])
      // console.log('导入表格',e)
      e.target.value = ""
    },
    /**
     * 计算总数
     */
    countResult() {
      let count = 0
      this.orderList.forEach((e, j) => {
        if (e.Commission) {
          count += parseInt(e.Commission)
        }
        // console.log(count,'计算总数',this.orderList)
        this.priceCount = count
      })
    },
    /**
     * 求最高价全票
     */
    filterDefault() {
      let arr = this.allTravelList.map(v => {
        return {
          Commission: v.Commission,
          Passwords: v.Passwords,
          couponSum: v.priceInfo[0].unitprice
        }
      })
      let index = 0
      console.log("最高价全票arr", arr, this.allTravelList)
      var num = arr[0].couponSum
      for (var i = 0; i < arr.length; i++) {
        if (arr[i].couponSum > num) {
          num = arr[i].couponSum
          index = i
        }
      }
      return {
        Commission: arr[index].Commission,
        Passwords: arr[index].Passwords,
        ruleName: "默认",
        couponSum: arr[index].couponSum,
        couponPrice: ""
      }
    },
    /**
     * 入园日期格式化
     */
    inDateVal(val) {
      this.inDate = val
      this.getTravelOrderItem(val)
    },
    /**
     * 计划到达时间格式化
     */
    arriveTimeVal(val) {
      this.arriveTime = val
    },
    /**
     * 返回首页
     * @date 2019-06-10
     */
    back() {
      this.$router.push({ path: "/booking/index" })
    },
    double(val) {
      if (val < 10) {
        return "0" + val
      } else {
        return val.toString()
      }
    }
  }
}
</script>

<style lang="less" scoped>
@import "../../assets/less/booking.less";
.page {
  &-title {
    .el-form-item {
      margin-bottom: 0;
    }
    .btn_file {
      // padding-left: 58px;
      .el-button {
        width: 120px;
        height: 45px;
        span {
          vertical-align: middle;
          i {
            margin-right: 5px;
            vertical-align: middle;
          }
        }
      }
    }
  }
  .flex_check_ct {
    display: inline-block;
    margin-right: 20px;
    // width: 80px;
    // flex: 1;
  }
  .el-form {
    height: 100%;
  }
  .select {
    padding: 0.3rem 1.3rem 0.3rem 0.3rem;
    background: #fff;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    .ellipsis {
      display: -webkit-box;
      overflow: hidden;
      text-overflow: ellipsis;
      word-break: break-all;
      -webkit-box-orient: vertical !important;
      -webkit-line-clamp: 1;
    }
    &_title {
      min-width: 200px;
      font-size: 12px;
    }
    i {
      top: 2px;
    }
    ul {
      top: 37px;
    }
  }
  .page-content {
    padding-bottom: 130px;
  }
  .cus_form {
    margin-bottom: 0px;
    margin-left: -130px;
  }
  .table_wrap {
    overflow-x: auto;
  }
  .table-order {
    > thead > tr > th {
      box-sizing: border-box;
    }
    > tbody > tr:nth-of-type(odd) {
      background-color: #fff;
    }
    > tbody > tr:nth-of-type(even) {
      background-color: #eeecec;
    }
    .el-form-item {
      margin-bottom: 0;
    }
    .ct {
      display: inline-block;
      min-width: 40px;
      &.empty{
        background: red;
      }
      .zhutu{
        width: 100px;
      }
    }
    .el-input {
      height: 36px;
      input {
        height: 100%;
      }
    }
    .el-dropdown {
      font-size: 1.1rem;
    }
  }
  .table-order-hover {
    > tbody > tr:hover {
      background-color: rgb(241, 241, 241);
    }
  }
  .add-order {
    margin: 1rem auto;
    color: #37b0eb;
    font-size: 25px;
    text-align: center;
    cursor: pointer;
    .el-button--large {
      padding: 12px 60px;
    }
  }
  .file_box {
    display: inline-block;
    position: relative;
    font-weight: 500;
    height: 45px;
    width: 120px;
    text-align: center;
    font-size: 14px;
    line-height: 45px;
    border-radius: 4px;
    overflow: hidden;
    color: #1eb1b1;
    background: #e0f7f7;
    border: 1px solid #1eb1b1;
    cursor: pointer;
    vertical-align: middle;
    i:hover {
      color: #fff;
    }
    &:hover {
      color: #fff;
      background-color: #1eb1b1;
      border-color: #1eb1b1;
      cursor: pointer;
    }
  }
  .file_box1 {
    margin: 0 20px 0 0px;
    i {
      font-size: 26px;
      color: #1b8a4e;
      vertical-align: middle;
      margin-right: 5px;
    }
  }
  .file_box2 {
    a {
      display: block;
      width: 100%;
      height: 100%;
      &:hover {
        color: #fff;
      }
    }
  }
  .file-btn {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    outline: none;
    background-color: transparent;
    filter: alpha(opacity=0);
    -moz-opacity: 0;
    -khtml-opacity: 0;
    opacity: 0;
    cursor: pointer;
  }
}
.el-dropdown-menu {
  max-height: 300px;
  overflow-y: auto;
}
.el-form-item__error {
  top: 0;
  height: 17px;
}
.el-dropdown {
  width: 100%;
  &-link {
    width: 90%;
    line-height: 40px;
    i {
      position: absolute;
      top: 13px;
      right: 0;
    }
  }
}
.ellipsis {
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-all;
  -webkit-box-orient: vertical !important;
  -webkit-line-clamp: 1;
}
</style>
